package com.strongmangames.DPM;

/**
 * ...Class representing one patch operation.
 * @author Rohan Feldmesser
 */

class Patch 
{

	public function new() 
	{
		
	}
	
	
	public List<Diff> diffs = new List<Diff>();
    public Int start1;
    public Int start2;
    public Int length1;
    public Int length2;

    /**
     * Emmulate GNU diff's format.
     * Header: @@ -382,8 +481,9 @@
     * Indicies are printed as 1-based, not 0-based.
     * @return The GNU diff string.
     */
    public function ToString():String
	{
      var coords1:String = null;
	  var coords2:String = null;
	  
      if (this.length1 == 0) {
        coords1 = this.start1 + ",0";
      } else if (this.length1 == 1) {
        coords1 = Std.string(this.start1 + 1);
      } else {
        coords1 = (this.start1 + 1) + "," + this.length1;
      }
      if (this.length2 == 0) {
        coords2 = this.start2 + ",0";
      } else if (this.length2 == 1) {
        coords2 = Std.string(this.start2 + 1);
      } else {
        coords2 = (this.start2 + 1) + "," + this.length2;
      }
		var text:String = "";
		text = "@@ -" + coords1 + " +" + coords2 + " @@\n";
		// Escape the body of the patch with %xx notation.
		for (aDiff:Diff in this.diffs) 
		{
			switch (aDiff.operation) 
			{
				case Operation.INSERT:
					text.Append('+');
					break;
				case Operation.DELETE:
					text.Append('-');
					break;
				case Operation.EQUAL:
					text.Append(' ');
					break;
			}
			
			text+=StringTools.urlEncode(aDiff.text).split('+').join('\n').split(' ').join('\n');
      }

      return diff_match_patch.unescapeForEncodeUriCompatability(text.toString());
    }
	
}